<% database_auth = Settings.authentication == 'database'
   menu = (is_admin? ? [link_to(icon('plus', 'Add'), new_organization_user_path, :remote => true, :class => 'btn btn-mini')] : nil) %>
<%= section_panel 'Users', :width => 'triple', :menu => menu do %>
  <%= list(@users,
           :new_link => (is_admin? ? link_to(icon('plus', 'Add User...'), new_organization_user_path, :remote => true) : nil),
           :menu     => [{:id => 'remove_user', :label => icon('trash', 'Remove', 'fa-fw'), :function => 'confirm_remove', :param_name => 'userIds'}],
           :toolbar  => {:sort_by   => [%w(Username username), ['Full Name', 'full_name'], %w(Email email), %w(Created created), ['Last Sign-In for Org', 'last_org_sign_in'], ['Last Sign-In for OneOps (Global)', 'last_oneops_sign_in']],
                         :filter_by => %w(username full_name email team)}) do |builder, user| %>
    <% builder.itemid user.id %>
    <% teams = (@user_teams[user.id] || []).sort_by {|t| t == Team::ADMINS ? '' : t}
       last_sign_in_at_for_current_org = user.last_sign_in_at_for_current_org
       me = user.username == current_user.username %>
    <% builder.attributes raw(%(username="#{user.username}" full_name="#{user.name}" email="#{user.email}" created="#{user.created_at}" last_org_sign_in="#{last_sign_in_at_for_current_org} || 0" last_oneops_sign_in="#{user.current_sign_in_at}" team="#{teams.join(', ')}")) %>
    <% builder.target link_to('', edit_organization_user_path(user), :remote => true) if is_admin? %>
    <% if database_auth || me %>
      <% builder.status do %>
        <% if database_auth %>
          <%= marker('unconfirmed', 'label-warning') if user.confirmation_token? %>
          <%= marker('locked', 'label-important') if user.locked_at? %>
          <%= marker('password reset', 'label-important') if user.reset_password_token? %>
        <% end %>
      <% end %>
    <% end %>
    <% builder.top do %>
      <span class="topname"><%= user.username %></span>
      <span class="topdesc"><%= user.name %> <small class="text-black"><%= user.email %></small></span>
    <% end %>
    <% builder.middle do %>
      <%= expandable_list(teams.map {|t| marker(t, t == Team::ADMINS ? 'label-info' : 'label-inverse')}, :visible_count => 7, :separator => ' ') %>
    <% end %>
    <% builder.bottom do %>
      created <b><%= time_ago_in_words(user.created_at) %></b>
      <% if database_auth && Settings.confirmation && user.confirmation_token.blank? %>
        - confirmed <b><%= time_ago_in_words(user.confirmed_at) %></b>
      <% end %>
      <% if user.confirmation_token? %>
        &nbsp;|&nbsp; confirmation email sent <b><%= time_ago_in_words(user.confirmation_sent_at) %></b>
      <% end %>
      &nbsp;|&nbsp; last org sign in: <b><%= last_sign_in_at_for_current_org ? time_ago_in_words(last_sign_in_at_for_current_org) : 'N/A' %></b>
      &nbsp;|&nbsp; last OneOps sign in: <b><%= user.current_sign_in_at ? time_ago_in_words(user.current_sign_in_at) : 'N/A' %></b>
    <% end %>
    <% builder.menu do %>
      <% if is_admin? && current_user.id != user.id %>
        <%= link_confirm_busy(icon('remove', 'Remove'),
                              :url     => organization_user_path(user),
                              :method  => :delete,
                              :confirm => 'Please confirm',
                              :body    => "Remove user '#{user.username}' from his organization?") %>

      <% end %>
    <% end %>
  <% end %>
<% end %>
<div id="reset_password_modal" class="modal hide"></div>
<div id="organization_user_modal" class="modal hide"></div>
<script>
  window.confirm_remove = function (actionId, user_ids) {
    window.bulk_remove_user_ids = $j.map(user_ids, function (i) {
      return "ids[]=" + i;
    }).join("&");
    render_modal("confirm_busy",
                 "<%= escape_javascript(render('base/shared/confirm_busy_block',
                                               {:modal_id => 'confirm_busy',
                                                :url      => remove_organization_users_path,
                                                :method   => :delete,
                                                :confirm  => 'Remove?',
                                                :body     => 'Confirm remove selected users?',
                                                :with     => 'window.bulk_remove_user_ids' })) %>");
  }
</script>
